package org.example.stopthreads;

/**
 * 最佳实践 catch方法的异常，优先选择抛
 * 最顶层的  try 的位置就是停止的位置
 */
public class RightWayStopThreadInProd implements Runnable {
    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("go");
                throwInMethod();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private void throwInMethod() throws InterruptedException {
        Thread.sleep(2000);
    }

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(new RightWayStopThreadInProd());
        thread.start();
        Thread.sleep(1);
        thread.interrupt();
    }
}
